package algorithm.union;

/**
 * @author cyt
 * @create 2020-09-05 16:20
 */
public class UnionQf extends UnionFind {

    public UnionQf(int cap) {
        super(cap);
    }

    @Override
    public void union(int a, int b) {
        int value_A = find(a);
        int value_B = find(b);

        if (value_A == value_B) {
            return;
        }
        for (int i = 0; i < parents.length; i++) {
            if (value_A == parents[i]) {
                parents[i] = value_B;
            }
        }
    }

    @Override
    public int find(int a) {
        rangeCheck(a);
        return parents[a];
    }

}
